package org.algorithm.Backtracking.leetcode90;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution2 {


    List<List<Integer>> res = new ArrayList<>();
    List<Integer> item = new ArrayList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        int n = nums.length;
        boolean [] used = new boolean[n];
        Arrays.fill(used,false);
        Arrays.sort(nums);
        backtracking(nums,used,0);
        return res;
    }

    private void backtracking(int [] nums,boolean [] used,int index){

        res.add(new ArrayList<>(item));

        for (int i = index; i < nums.length ; i++) {
            if (i>0 && nums[i-1]==nums[i] && !used[i-1]){
                continue;
            }

            used[i]=true;
            item.add(nums[i]);
            backtracking(nums,used,i+1);
            used[i]=false;
            item.remove(item.size()-1);
        }
    }
}
